﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MaLu.Data;
using System.Data;
using MaLu.Common;
using System.Xml;
using MaLu.BusiLogic;
using MaLu.Framework.Common;

namespace MaluJg.web.Visite
{
    public partial class item4 : VisitePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {


                if (VisiteID != Guid.Empty)
                {

                    DataTable dt = SqlHelper.Instance.ExecuteDataTable(Provider, "select * from Visite where id ='" + VisiteID.ToString() + "'");
                    if (dt.Rows.Count > 0)
                    {


                        lbYear2.Text = lbYear.Text = dt.Rows[0]["Year"].ToString();
                        lbMonth2.Text = lbMonth.Text = dt.Rows[0]["Month"].ToString();

                    }
                }
                BindData();
            }
        }
        private void BindData()
        {
            string projectname = string.Empty;
            string content = BLVisite.GetVisiteItem(Provider, VisiteID, "item4.aspx", ref projectname);
            if (content != string.Empty)
            {
                XmlDocument document = new XmlDocument();
                document.LoadXml(content);

                XmlElement xe = document.SelectSingleNode("//月1") as XmlElement;
                tbMonth.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//责任部门") as XmlElement;
                tbDept1.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//走访单位负责人签字") as XmlElement;
                tbSign.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//年2") as XmlElement;
                tbYear2.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//月2") as XmlElement;
                tbMonth2.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//日2") as XmlElement;
                tbDay2.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//月3") as XmlElement;
                tbMonth3.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//问题现状") as XmlElement;
                tbProSituation.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//整改意见") as XmlElement;
                tbSug.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//延期时间") as XmlElement;
                tbDelayTime.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//延期原因") as XmlElement;
                tbDelayReason.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//分管领导意见") as XmlElement;
                tbLeaderSug.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//责任单位部门") as XmlElement;
                tbDept2.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//年4") as XmlElement;
                tbYear4.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//月4") as XmlElement;
                tbMonth4.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//日4") as XmlElement;
                tbDay4.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//意见") as XmlElement;
                XmlNodeList xnl = xe.ChildNodes;
                if (xnl.Count >= 1)
                {
                    tbPro00.Text = xnl[0].Attributes["p1"].Value;
                    tbPro01.Text = xnl[0].Attributes["p2"].Value;
                    tbPro02.Text = xnl[0].Attributes["p3"].Value;                   
                }
                if (xnl.Count >= 2)
                {
                    tbPro10.Text = xnl[1].Attributes["p1"].Value;
                    tbPro11.Text = xnl[1].Attributes["p2"].Value;
                    tbPro12.Text = xnl[1].Attributes["p3"].Value;
                }
                if (xnl.Count >= 3)
                {
                    tbPro20.Text = xnl[2].Attributes["p1"].Value;
                    tbPro21.Text = xnl[2].Attributes["p2"].Value;
                    tbPro22.Text = xnl[2].Attributes["p3"].Value;
                }
                if (xnl.Count >= 4)
                {
                    tbPro30.Text = xnl[3].Attributes["p1"].Value;
                    tbPro31.Text = xnl[3].Attributes["p2"].Value;
                    tbPro32.Text = xnl[3].Attributes["p3"].Value;
                }
                if (xnl.Count >= 5)
                {
                    tbPro40.Text = xnl[4].Attributes["p1"].Value;
                    tbPro41.Text = xnl[4].Attributes["p2"].Value;
                    tbPro42.Text = xnl[4].Attributes["p3"].Value;
                }
                if (xnl.Count >= 6)
                {
                    tbPro50.Text = xnl[5].Attributes["p1"].Value;
                    tbPro51.Text = xnl[5].Attributes["p2"].Value;
                    tbPro52.Text = xnl[5].Attributes["p3"].Value;
                }
                if (xnl.Count >= 7)
                {
                    tbPro60.Text = xnl[6].Attributes["p1"].Value;
                    tbPro61.Text = xnl[6].Attributes["p2"].Value;
                    tbPro62.Text = xnl[6].Attributes["p3"].Value;
                }
                if (xnl.Count >=8)
                {
                    tbPro70.Text = xnl[7].Attributes["p1"].Value;
                    tbPro71.Text = xnl[7].Attributes["p2"].Value;
                    tbPro72.Text = xnl[7].Attributes["p3"].Value;
                }
                

            }
        }
        private string FillContent()
        {
            string template = BLVisite.GetTemplate(Provider, "item4.aspx");
            if (template != string.Empty)
            {

                XmlDocument document = new XmlDocument();
                document.LoadXml(template);

                XmlElement xe = document.SelectSingleNode("//月1") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbMonth.Text.Trim());
                xe = document.SelectSingleNode("//责任部门") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbDept1.Text.Trim());
                xe = document.SelectSingleNode("//走访单位负责人签字") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbSign.Text.Trim());
                xe = document.SelectSingleNode("//年2") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbYear2.Text.Trim());
                xe = document.SelectSingleNode("//月2") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbMonth2.Text.Trim());
                xe = document.SelectSingleNode("//日2") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbDay2.Text.Trim());
                xe = document.SelectSingleNode("//意见") as XmlElement;

                XmlNode xn = document.CreateElement("p");
                XmlAttribute xattr = document.CreateAttribute("p1");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro00.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("p2");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro01.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("p3");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro02.Text));
                xn.Attributes.Append(xattr);               
                xe.AppendChild(xn);

                 xn = document.CreateElement("p");
                 xattr = document.CreateAttribute("p1");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro10.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("p2");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro11.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("p3");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro12.Text));
                xn.Attributes.Append(xattr);
                xe.AppendChild(xn);

                xn = document.CreateElement("p");
                xattr = document.CreateAttribute("p1");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro20.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("p2");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro21.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("p3");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro22.Text));
                xn.Attributes.Append(xattr);
                xe.AppendChild(xn);

                xn = document.CreateElement("p");
                xattr = document.CreateAttribute("p1");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro30.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("p2");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro31.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("p3");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro32.Text));
                xn.Attributes.Append(xattr);
                xe.AppendChild(xn);

                xn = document.CreateElement("p");
                xattr = document.CreateAttribute("p1");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro40.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("p2");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro41.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("p3");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro42.Text));
                xn.Attributes.Append(xattr);
                xe.AppendChild(xn);

                xn = document.CreateElement("p");
                xattr = document.CreateAttribute("p1");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro50.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("p2");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro51.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("p3");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro52.Text));
                xn.Attributes.Append(xattr);
                xe.AppendChild(xn);

                xn = document.CreateElement("p");
                xattr = document.CreateAttribute("p1");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro60.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("p2");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro61.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("p3");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro62.Text));
                xn.Attributes.Append(xattr);
                xe.AppendChild(xn);

                xn = document.CreateElement("p");
                xattr = document.CreateAttribute("p1");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro70.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("p2");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro71.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("p3");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbPro72.Text));
                xn.Attributes.Append(xattr);
                xe.AppendChild(xn);

                xe = document.SelectSingleNode("//月3") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbMonth3.Text.Trim());
                xe = document.SelectSingleNode("//问题现状") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbProSituation.Text.Trim());
                xe = document.SelectSingleNode("//整改意见") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbSug.Text.Trim());
                xe = document.SelectSingleNode("//延期时间") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbDelayTime.Text.Trim());
                xe = document.SelectSingleNode("//延期原因") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbDelayReason.Text.Trim());
                xe = document.SelectSingleNode("//分管领导意见") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbLeaderSug.Text.Trim());
                xe = document.SelectSingleNode("//责任单位部门") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbDept2.Text.Trim());

                xe = document.SelectSingleNode("//年4") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbYear4.Text.Trim());
                xe = document.SelectSingleNode("//月4") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbMonth4.Text.Trim());
                xe = document.SelectSingleNode("//日4") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbDay4.Text.Trim());
                return document.OuterXml;
            }
            else
                return string.Empty;
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            BLVisite.SaveVisiteItem(Provider, VisiteID, "item4.aspx", FillContent());


            string msg = "已保存!";

            string script = "<script>alert('" + msg + "');</script>";
            ClientScript.RegisterStartupScript(this.GetType(), "Close", script);


        }
    }
}
